using System.Collections.Generic;
using System.IO;
using UnityEngine;
/// <summary>
/// AssetBundle资源管理器
/// </summary>
public partial class HFABMgr : AbsHFSingleMonoBehaviour<HFABMgr>
{
    /// <summary>
    /// 总加载进度
    /// </summary>
    public float progress { get {
            float val = 0;
            int num = 0;
            foreach (var key in mHFAssetLoaderMaping.Values)
            {
                if (key.assetLoadResult != enHFAssetLoadResult.NotStarted)
                {
                    val += key.progress;
                    num++;
                }
            }
            return Mathf.Clamp01(val / num);
    } }

    /// <summary>
    /// 资源加载项
    /// </summary>
    Dictionary<int, HFAssetLoader> mHFAssetLoaderMaping = new Dictionary<int, HFAssetLoader>();

    /// <summary>
    /// 资源属性映射
    /// </summary>
    Dictionary<int, HFAssetBundlePathAttribute> mHFAssetBundlePathAttributeMaping = new Dictionary<int, HFAssetBundlePathAttribute>();

    /// <summary>
    /// 回收AssetLoad
    /// </summary>
    void OnRecycleAssetLoad()
    {        
        foreach (var loader in mHFAssetLoaderMaping.Values)
        {
            GameObject.Destroy(loader.gameObject);
        }
        mHFAssetLoaderMaping.Clear();
        mHFAssetBundlePathAttributeMaping.Clear();
    }


    /// <summary>
    /// 开始加载
    /// </summary>
    /// <param name="_result">资源结果</param>
    void OnStartLoad(AbsHFAssetRequest _result)
    {
        HFAssetBundlePathAttribute attr = OnGetAbpAttrByCache(_result.abPathAttr);
        HFAssetLoader loader = null;
        if (!mHFAssetLoaderMaping.TryGetValue(attr.assetBundleKey, out loader))
        {
            loader = new HFAssetLoader(attr, this, OnLoadInMemory, OnGetDependencyAbpAttr,OnGetLoader);
            mHFAssetLoaderMaping[attr.assetBundleKey] = loader;
        }
        loader.AddRequest(_result);
        loader.LoadAsset();
    }
    /// <summary>
    /// 重新加载资源
    /// </summary>
    /// <param name="_result"></param>
    void OnReStart(AbsHFAssetRequest _result)
    {
        HFAssetBundlePathAttribute attr = OnGetAbpAttrByCache(_result.abPathAttr);
        HFAssetLoader loader = null;
        if (mHFAssetLoaderMaping.TryGetValue(attr.assetBundleKey, out loader))
        {
            loader.ReStartLoadAsset(_result);
        }

    }
    /// <summary>
    /// 获得指定Loader
    /// </summary>
    /// <param name="_assetBundleKey">assetBundleKey</param>
    /// <returns>Loader</returns>
    HFAssetLoader OnGetLoader(int _assetBundleKey)
    {
        HFAssetLoader loader = null;
        mHFAssetLoaderMaping.TryGetValue(_assetBundleKey, out loader);
        return loader;
    }

    /// <summary>
    /// 从缓存拿指定资源属性
    /// </summary>
    /// <param name="_attr">资源属性</param>
    /// <returns>缓存资源属性</returns>
    HFAssetBundlePathAttribute OnGetAbpAttrByCache(HFAssetBundlePathAttribute _attr)
    {
        HFAssetBundlePathAttribute result = null;
        if (!mHFAssetBundlePathAttributeMaping.TryGetValue(_attr.assetBundleKey, out result))
        {
            result = _attr.Copy();
            mHFAssetBundlePathAttributeMaping[_attr.assetBundleKey] = result;
        }
        return result;
    }

    /// <summary>
    /// 获得依赖项的资源属性
    /// </summary>
    /// <param name="_depManifestName">依赖项Manifest名称</param>
    /// <returns>资源属性</returns>
    HFAssetBundlePathAttribute OnGetDependencyAbpAttr(string _depManifestName)
    {
        HFAssetBundlePathAttribute result = null;
        int key = _depManifestName.HFRemoveAssetBundleManifestNameHash().HFAssetBundleKey();
        if (!mHFAssetBundlePathAttributeMaping.TryGetValue(key, out result))
        {
            string abName = _depManifestName.HFRemoveAssetBundleManifestNameHash().HFRemoveAssetBundleExt(HFGlobalUtility.assetBundleNameSuffixExt);
            string name = Path.GetFileNameWithoutExtension(abName);
            var spa = HFGlobal_SpriteAtlas.Instance.GetSpriteAtlasPath(name);
            if (spa == null)
            {
                enHFLanguageRootFolder rf = default;
                string rp = string.Empty;
                if (HFLanguageHelper.ResolvePath(abName, out rf, out rp))
                {
                    result = new HFAssetBundlePathAttribute(rf, rp);
                }
                else
                {
                    result = new HFAssetBundlePathAttribute(rf, rp, true);//这里需要特殊处理下。
                }
            }
            else
            {
                result = spa;
            }
            OnCheckAssetBundlePathAttributeLanguage(result);
            mHFAssetBundlePathAttributeMaping[key] = result;
        }
        return result;
    }
}
